package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.ProgramaService;
import co.gov.medellin.ssti.sub.model.vo.ProgramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

@Component
public class ModProgramaAction extends Action {
	@Autowired
	ProgramaService programaServiceImpl2;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String valor1 = null, valor2 = null, valor3 = null, valor4 = null, valor5 = null;
		String valor6 = null, valor7 = null, valor8 = null, valor9 = null, valor10 = null;
		String valor11 = null;

		String strprog = request.getParameter("nombrePrograma");
		HttpSession session = request.getSession();

		try {
			valor1 = request.getParameter("codePrograma");
		} catch (Exception e) {
			valor1 = "";
		}
		try {
			valor2 = request.getParameter("objetivo");
		} catch (Exception e) {
			valor2 = "";
		}
		try {
			valor3 = request.getParameter("descripcion");
		} catch (Exception e) {
			valor3 = "";
		}
		try {
			valor4 = request.getParameter("area");
		} catch (Exception e) {
			valor4 = "";
		}
		try {
			valor5 = request.getParameter("admincodigo");
		} catch (Exception e) {
			valor5 = "";
		}
		try {
			valor10 = request.getParameter("codadmin");
		} catch (Exception e) {
			valor10 = "";

		}
		try {
			valor6 = request.getParameter("poblacion");
		} catch (Exception e) {
			valor6 = "";
		}
		try {
			valor7 = request.getParameter("desde");
		} catch (Exception e) {
			valor7 = "";
		}
		try {
			valor8 = request.getParameter("hasta");
		} catch (Exception e) {
			valor8 = "";
		}
		try {
			valor9 = request.getParameter("vigencia");
			if (valor9.equals("I")) {
				session.setAttribute("Programa", strprog);

				return mapping.findForward("success");
			}
		} catch (Exception e) {
			valor9 = "";
		}
		try {
			valor11 = request.getParameter("paraEmpresa");
		} catch (Exception e) {
			valor8 = "";
		}

		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);

		ProgramaVO programaVO = new ProgramaVO();

		if (valor5.equalsIgnoreCase("")) {
			programaVO.setCodeUsuario(Long.valueOf(valor10));
		} else {
			programaVO.setCodeUsuario(Long.valueOf(valor5));
		}
		programaVO.setCodeArea(Long.valueOf(valor4));
		programaVO.setNombre(strprog);
		programaVO.setDescripcion(valor3);
		programaVO.setObjetivo(valor2);
		programaVO.setPoblacionObjetivo(valor6);
		programaVO.setDesdeVigencia(valor7);
		programaVO.setHastaVigencia(valor8);
		programaVO.setVigencia(valor9);
		programaVO.setCampoA4(valor11);
		programaVO.setUsuarioModifica(user.getName());
		programaVO.setTerminalModifica(strTerminal);
		programaVO.setCode(Long.valueOf(valor1));

		try {
			int modificar = programaServiceImpl2.update(programaVO);
			if (modificar > 0) {
				session.setAttribute("mensajes",
						"El programa fue modificado correctamente!!!");
				session.setAttribute("primeraVez", null);
			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
			session.setAttribute("mensajes",
					"El programa no se pudo modificar.");
		}

		return mapping.findForward("success2");
	}
}